//int1 (PD3) zero crossing detector
//OC0 (PB3) TRIAC driver
//portC sebagai 7segment
//PD4 - PD6 7seg selector

//64,32,16
/*sbit LCD_RS at PORTC2_bit;//23
sbit LCD_EN at PORTC0_bit;//pin 25
sbit LCD_D7 at PORTC7_bit;//29
sbit LCD_D6 at PORTC6_bit;//28
sbit LCD_D5 at PORTC5_bit;//27
sbit LCD_D4 at PORTC4_bit;//26

sbit LCD_RS_Direction at DDC2_bit;
sbit LCD_EN_Direction at DDC0_bit;
sbit LCD_D7_Direction at DDC7_bit;
sbit LCD_D6_Direction at DDC6_bit;
sbit LCD_D5_Direction at DDC5_bit;
sbit LCD_D4_Direction at DDC4_bit;*/

volatile int angka[10] = {239,140,219,222,188,126,127,204,255,254};
short int P;
volatile short int f = 0;
volatile short int count = 0;

volatile short int rat;
volatile short int pul;
volatile short int sat;

//prescaler buat PWM 256

void zero_cross();
void cal_freq();
void main(){

    //inisialisasi
    PUD_bit = 0;//enable pull up
    SREG_I_bit = 1;//enable interrupt
    //portb
    DDB3_bit = 1;
    
    //portd
    PORTD3_bit = 1;
    DDD3_bit = 0;//input
    MCUCR.ISC10 = 1;//pin INT1 sbg logical change interrupt
    //MCUCR.ISC11 = 1;
    
    INT1_bit = 1; //INT1 enable

    //timer 16bit
    TCCR1B = (1 << CS10) | (1 << CS11) | (1<<WGM12);
    //CS10_bit = 1;
    //CS11_bit = 1;//prescaler 64
    OCR1AH = 0x7A;
    OCR1AL = 0x11;
    //WGM12_bit = 1; //CTC mode
    OCIE1A_bit = 1;//enable interrupt
    
    DDRC = 0xFF;//output buat seven seg
    DDRD.B4 = 1;
    DDRD.B5 = 1;
    DDRD.B6 = 1;
    

mains:
            PORTC = angka[sat];
            PORTD.B6 = 1;
            Delay_ms(9);
            PORTD.B6 = 0;//blanking?
            PORTC = angka[pul];
            Delay_us(1);
            PORTD.B5 = 1;
            Delay_ms(9);
            PORTD.B5 = 0;//blanking?
            PORTC = angka[rat];
            Delay_us(1);

            PORTD.B4 = 1;
            Delay_ms(9);
            PORTD.B4 = 0;//blanking?
            Delay_us(1);
            //P = P + 1;

    goto mains;
}

void cal_freq() org IVT_ADDR_TIMER1_COMPA{

    f = count;
    count = 0;
    rat = f / 100;
    pul = f % 100 / 10;
    sat = f % 10;
}
void zero_cross () org IVT_ADDR_INT1{
    count++;
    //Delay_ms(7);
    PORTB3_bit = 1;
    Delay_us(3);
    PORTB3_bit = 0;
}
